package com.ndp.fb.mq.consumer.business;

import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.model.fbagent.param.ApiCallBackTarget;
import com.ndp.fb.mq.consumer.AbstractPullConsumer;
import com.ndp.fb.mq.consumer.business.syncbusiness.AdGroupGCBusiness;
import com.ndp.fb.mq.consumer.business.syncbusiness.SyncAdImageBusiness;
import com.ndp.fb.mq.consumer.business.syncbusiness.SyncAdVideoBusiness;
import com.ndp.fb.redis.api.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by Mikey.zhaopeng on 2015/3/4 17:42.
 * Email mikey.zhao@ndpmedia.com
 * 业务数据同步
 */
@Component
public class BuesinessConsumerServiceImpl extends AbstractPullConsumer {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RedisClient redisClient;


    @Autowired
    private SyncAdImageBusiness syncAdImageBusiness;
    
    @Autowired
    private AdGroupGCBusiness fetchAdGroupBusiness;

    @Autowired
    private SyncAdVideoBusiness syncResourceBusiness;
    

    @Override
    protected void consume(String tag, String keys, Object message, Map<String, String> equipment) {
        logger.debug("SyncBuesinessConsumerServiceImpl tag >>>> " + tag + " keys >>>" + keys + " message >>> " + message);
        if (message == null || "".equals(message.toString())) {
            logger.warn("SyncBuesinessConsumerServiceImpl error,message is null");
        } else {
            Map<String, Object> messageBody = this.getMessageBody(message.toString());
            if (messageBody == null) {
                logger.warn("SyncBuesinessConsumerServiceImpl error,MessageBody is null");
            } else {
                Object body = messageBody.get("body");
                Map<String, String> equipmentMap = (Map<String, String>) messageBody.get("equipment");
                ApiCallBackTarget.PublishResource target = (ApiCallBackTarget.PublishResource) ApiCallBackTarget.getTargetType(tag);
                switch (target) {
                    case FetchOneAdGroup:
                        fetchAdGroupBusiness.fetchOneCallBack(body.toString(), equipmentMap);
                        break;
                    case GetImageHash:
                        syncAdImageBusiness.getImageHashCallback(body.toString(), equipmentMap);
                        break;
                    case StartUploadAdVideo:
                        syncResourceBusiness.startUploadAdVideoCallback(body.toString(), equipmentMap);
                        break;
                    case TransferUploadAdVideo:
                        syncResourceBusiness.transferUploadAdVideoCallback(body.toString(), equipmentMap);
                        break;
                    case FinishUploadAdVideo:
                        syncResourceBusiness.finishUploadAdVideoCallback(body.toString(), equipmentMap);
                        break;
                    case FetchAdImage:
                        //从facebook拉取adimage数据
                        syncAdImageBusiness.syncBusiness(body.toString(), equipmentMap);
                        break;
                    default:
                        logger.warn("不识别的消息tag >>>>" + tag + " ; message >>> " + message);
                }
            }
        }
    }

    @Override
    public String getTopicName() {
        return ConfigConst.MQ_SERVICES_PUBLISH_RESOURCE_TOPIC;
    }

    /**
     * message存储的是uuid,根据uuid去除在缓存中存储的messageBody
     *
     * @return
     */
    private Map<String, Object> getMessageBody(String key) {
        Map<String, Object> messageBody = null;
        if (redisClient.hasKey(key)) {
            messageBody = (Map<String, Object>) redisClient.get(key);
            redisClient.delete(key);
        }
        return messageBody;
    }
}
